package org.jeecg.modules.finan.controller;

import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.finan.entity.FinanTenderInfo;
import org.jeecg.modules.finan.service.IFinanTenderInfoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.online.config.exception.BusinessException;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 标段信息表
 * @Author: jeecg-boot
 * @Date: 2021-02-27
 * @Version: V1.0
 */
@Api(tags = "标段信息表")
@RestController
@RequestMapping("/finanTenderInfo")
@Slf4j
public class FinanTenderInfoController extends JeecgController<FinanTenderInfo, IFinanTenderInfoService> {
    @Autowired
    private IFinanTenderInfoService finanTenderInfoService;
    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    /**
     * 分页列表查询
     *
     * @param finanTenderInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "标段信息表-分页列表查询")
    @ApiOperation(value = "标段信息表-分页列表查询", notes = "标段信息表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(FinanTenderInfo finanTenderInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<FinanTenderInfo> page = new Page<FinanTenderInfo>(pageNo, pageSize);
//		QueryWrapper<FinanTenderInfo> queryWrapper = QueryGenerator.initQueryWrapper(finanTenderInfo, req.getParameterMap());
//		IPage<FinanTenderInfo> pageList = finanTenderInfoService.page(page, queryWrapper);
        IPage<FinanTenderInfo> pageList = finanTenderInfoService.selectFinanTenderInfoList(page);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param finanTenderInfo
     * @return
     */
    @AutoLog(value = "标段信息表-添加")
    @ApiOperation(value = "标段信息表-添加", notes = "标段信息表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody FinanTenderInfo finanTenderInfo) {
        finanTenderInfoService.save(finanTenderInfo);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param finanTenderInfo
     * @return
     */
    @AutoLog(value = "标段信息表-编辑")
    @ApiOperation(value = "标段信息表-编辑", notes = "标段信息表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody FinanTenderInfo finanTenderInfo) {
        finanTenderInfoService.updateById(finanTenderInfo);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "标段信息表-通过id删除")
    @ApiOperation(value = "标段信息表-通过id删除", notes = "标段信息表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        finanTenderInfoService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "标段信息表-批量删除")
    @ApiOperation(value = "标段信息表-批量删除", notes = "标段信息表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.finanTenderInfoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "标段信息表-通过id查询")
    @ApiOperation(value = "标段信息表-通过id查询", notes = "标段信息表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        FinanTenderInfo finanTenderInfo = finanTenderInfoService.getById(id);
        if (finanTenderInfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(finanTenderInfo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param finanTenderInfo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FinanTenderInfo finanTenderInfo) {
        return super.exportXls(request, finanTenderInfo, FinanTenderInfo.class, "标段信息表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FinanTenderInfo.class);
    }

    @AutoLog(value = "下载文件")
    @GetMapping(value = "/download")
    public ResponseEntity download(String fileName, String filepath) throws IOException {
        fileName = "投标保函.docx";
        // 获取文件名称，中文可能被URL编码
        fileName = URLDecoder.decode(fileName, "UTF-8");
        // 获取本地文件系统中的文件资源
        FileSystemResource resource = new FileSystemResource(upLoadPath + "//" + fileName);

        // 解析文件的 mime 类型
        String mediaTypeStr = URLConnection.getFileNameMap().getContentTypeFor(fileName);
        // 无法判断MIME类型时，作为流类型
        mediaTypeStr = (mediaTypeStr == null) ? MediaType.APPLICATION_OCTET_STREAM_VALUE : mediaTypeStr;
        // 实例化MIME
        MediaType mediaType = MediaType.parseMediaType(mediaTypeStr);

        HttpHeaders headers = new HttpHeaders();
        // 下载之后需要在请求头中放置文件名，该文件名按照ISO_8859_1编码。
        String filenames = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        headers.setContentDispositionFormData("attachment", filenames);
        headers.setContentType(mediaType);
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(resource.getInputStream().available())
                .body(resource);
    }

    @GetMapping("/getFinanTenderInfoByTenderProjectGuid")
    @ApiOperation("根据项目id获取投标信息")
    public Result<?> getFinanTenderInfoByTenderProjectGuid(@RequestParam("tenderProjectGuid") String tenderProjectGuid) {

        QueryWrapper<FinanTenderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TENDER_PROJECT_GUID", tenderProjectGuid);
        List<FinanTenderInfo> list = finanTenderInfoService.list(queryWrapper);
        return Result.OK(list);
    }
}
